import {createApp} from 'vue'
import {Base64} from 'js-base64'//加密模块
//管理员账号ID,正整数
function checkName(ID){
    return new RegExp('^[1-9]\\d*$').test(ID)
}
//验证码API
const imagPath = document.getElementById('u28_img').src
//验证码刷新方法
let kaptchaFuctions = {
    url:imagPath,
    imgFlush:function (){
        document.getElementById('u28_img').src = imagPath + "?p=" + Math.random()
    }
}

const app = createApp({
    data() {
        return {
            adminID: '',//存储username
            password:'',//存储密码
            choice: '',//记住密码选项
            code:'',//存储验证码
        }
    },

    methods:{
        //登录的方法
        login(event){
            if(this.adminID === null){
                layer.msg("管理员账号不能为空",null)
            }else if(this.password === ''){
                layer.msg("密码不能为空",null)
            }else if(!checkName(this.adminID)){
                layer.msg("无效管理员账号",null)
            }else if(this.code === '') {
                layer.msg("验证码不能为空",null)
             } else{
                let url = document.getElementById('loginUrl').action
                axios({//发送异步请求
                    url:url,
                    method:'post',
                    data:{
                        adminLoginInfo:{
                            adminID:this.adminID,
                            password:Base64.encode(this.password),
                            kaptchaCode:this.code,
                            choice:this.choice
                        }
                    }
                }).then(function (response) {
                    let result = JSON.parse(JSON.stringify(response.data))
                    if(result.code === 1) {
                        layer.msg("登录成功！",null)
                        setTimeout(function(){
                            window.location.href = document.getElementById('enterUrl').action
                        },2000);

                    }else if(result.code === 0){
                        layer.msg("验证码错误！",null)
                        jQuery("#u8_input").val('')//清空·验证码输入框
                        kaptchaFuctions.imgFlush()
                    }else{
                        layer.msg("账号或密码错误！",null)
                        //验证码，账号，密码输入框全清空
                        jQuery("#u8_input").val('')
                        jQuery("#u12_input").val('')
                        jQuery("#u21_input").val('')
                        jQuery("#choice").val(false)
                        kaptchaFuctions.imgFlush()

                    }

                })
            }

        },
        freshKaptcha(){
            kaptchaFuctions.imgFlush()
        }


    }


}).mount('#base')



